package com.tianyang.yukuaifuzs.common.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.tianyang.yukuaifuzs.business.auth.bean.AreaCityOutput;
import com.tianyang.yukuaifuzs.common.view.SelectPopWindow;
import com.tianyang.yukuaifuzs.common.view.SingleSelectView;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：地区单列选择
 * Created by 薛勇军 on 2023/10/18.
 */
public class AreaCheckListener {

    private final Context context;

    public AreaCheckListener(Context context) {
        this.context = context;
    }

    /**
     * 处理单选
     *
     * @param parentView 父布局
     * @param dataList 数据集
     * @param iWheelDataChangeCallback 回调
     */
    public void handleCheck(View parentView, String title, List<AreaCityOutput> dataList, String showData,
                            IWheelDataChangeCallback iWheelDataChangeCallback) {
        if (parentView == null || dataList == null) {
            return;
        }
        int position = 0;
        List<String> strings = new ArrayList<>();
        if (!dataList.isEmpty()) {
            for (int index=0;index<dataList.size();index++) {
                strings.add(dataList.get(index).getRegionName());
                if (!TextUtils.isEmpty(showData) && TextUtils.equals(showData, dataList.get(index).getRegionName())) {
                    position = index;
                }
            }
        }
        SingleSelectView ssView = new SingleSelectView(context, strings, position);
        SelectPopWindow pw = new SelectPopWindow((Activity) context, parentView, ssView.getView(), title);
        MyClickListener myClickListener = new MyClickListener(pw, dataList, position,
                iWheelDataChangeCallback);
        ssView.setWheelFinisheListener(myClickListener);
        pw.setConfirmListener(myClickListener);
    }

    class MyClickListener implements SingleSelectView.OnWheelFinisheListener, View.OnClickListener {

        private final List<AreaCityOutput> dataList;

        private int position;

        private final SelectPopWindow pw;

        private final IWheelDataChangeCallback iWheelDataChangeCallback;

        public MyClickListener(SelectPopWindow pw,  List<AreaCityOutput> dataList,
                               int position, IWheelDataChangeCallback iWheelDataChangeCallback) {
            this.pw = pw;
            this.dataList = dataList;
            this.position = position;
            this.iWheelDataChangeCallback = iWheelDataChangeCallback;
        }

        @Override
        public void onClick(View v) {
            if (position != -1 && dataList != null && dataList.size() > position) {
                if (iWheelDataChangeCallback != null) {
                    iWheelDataChangeCallback.wheelDataChangeCallback(dataList.get(position));
                }
            }
            pw.dismiss();
        }

        @Override
        public void OnChange(String currentValue, int currentIndex) {
            position = currentIndex;
        }
    }

    public interface IWheelDataChangeCallback {

        void wheelDataChangeCallback(AreaCityOutput output);
    }
}
